package main

import (
	"fmt"
	"github.com/streadway/amqp"
	"log"
	"os"
	"strings"
)

type Query struct {
	name       string
	durable    bool
	autoDelete bool
	exclusive  bool
	noWait     bool
	args       amqp.Table
}

var query = Query{
	name:       "",
	durable:    false,
	autoDelete: false,
	exclusive:  false,
	noWait:     false,
	args:       nil,
}

func failOnError(err error, msg string) {
	if err != nil {
		log.Fatalf("%s: %s", msg, err)
		panic(fmt.Sprintf("%s: %s", msg, err))
	}
}

func main() {
	conn, err := amqp.Dial("amqp://guest:guest@localhost:5672/")
	failOnError(err, "amqp.Dial err")
	defer conn.Close()

	ch, err := conn.Channel()
	failOnError(err, "Failed to open a channel")
	defer ch.Close()

	err = ch.ExchangeDeclare(
		"los",
		"fanout",
		true,
		false,
		false,
		false,
		nil,
	)
	failOnError(err, "ch.ExchangeDeclare err")

	err = ch.Publish(
		"los",
		"",
		false,
		false,
		amqp.Publishing{ContentType: "text/plain",
			Body: []byte(bodyFrom(os.Args)),
		})
}

func bodyFrom(args []string) string {
	var s string
	if (len(args) < 2) || os.Args[1] == "" {
		s = "hello"
	} else {
		s = strings.Join(args[1:], " ")
	}
	return s
}
